package hiq.math.complex;

public class Complex {
	
	public double re;
	public double im;
	
	/**
	 * Constructor.
	 * @param re
	 * @param im
	 */
	private Complex(double re, double im){
		this.re = re;
		this.im = im;
	}
	
	/**
	 * Creates a new complex number.
	 * @param re
	 * @param im
	 * @return
	 */
	public static Complex create (double re, double im){
		return new Complex(re, im);
	}	
	
	/**
	 * Create a number from an exponential form.
	 * @param abs
	 * 			absolute value.
	 * @param arg
	 * 			argument.
	 * @return
	 */
	public static Complex expCreate(double abs, double arg){
		return new Complex(abs * Math.cos(arg), abs * Math.sin(arg));
		
	}
	
	/**
	 * Return absolute value.
	 * @return
	 */
	public double abs(){
		return Math.sqrt(Math.pow(re, 2) + Math.pow(im, 2));
	}
	
	/**
	 * Return argument of real number
	 * @return
	 */
	public double arg(){
		return Math.atan(im / re);
	}
	
	@Override
	/**
	 * Return string representation of a number.
	 */
	public String toString() {
		String res;
		if (re == 0){
			res = this.im + "i";
		} else if (im == 0){
			res = this.re + "";
		} else {
			res = this.re + " + " + this.im + "i";
		}
		return res;
	}
	
}
